<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="平台类型" prop="">
      <el-select v-model="dataForm.hotSrc" placeholder="平台类型">
        <el-option
        v-for="type in dataForm.hotSrcOption"
        :key="type.value"
        :label="type.label"
        :value="type.value">
        </el-option>
	  </el-select>
    </el-form-item>
    <el-form-item label="热词类型" prop="">
      <el-select v-model="dataForm.hotType" placeholder="热词类型">
        <el-option
        v-for="type in dataForm.hotTypeOption"
        :key="type.value"
        :label="type.label"
        :value="type.value">
        </el-option>
	  </el-select>
      <el-input v-if="dataForm.hotType === 2" v-model="dataForm.hotUrl" placeholder="请输入热词跳转路径"></el-input>
    </el-form-item>
    <el-form-item label="类型" prop="" v-if="this.dataForm.id === 0">
      <el-select v-model="dataForm.typeId" placeholder="类型">
        <el-option
        v-for="type in dataForm.typeIdOption"
        :key="type.value"
        :label="type.label"
        :value="type.value">
        </el-option>
	  </el-select>
    </el-form-item>
    <el-form-item label="热词内容" prop="hotWords">
      <input style="width: 300px" v-model="dataForm.hotWords" placeholder="热词内容" class="el-input__inner" @input="widthCheck($event.target, dataForm.typeId)"/><br/>
      <span v-if="dataForm.typeId === 1">-------------热搜标签文字不能超过8字-------------</span>
      <span v-if="dataForm.typeId === 2">-------------内部广告最多不能超过15字-------------</span>
    </el-form-item>
    <el-form-item label="排序(倒序)" prop="hotSort">
      <el-input type="number" style="width: 200px" v-model="dataForm.hotSort" placeholder="排序(倒序)"></el-input>
    </el-form-item>
    <el-form-item label="logo图片" prop="logoImg">
      <el-upload
      :action="this.$http.adornUrl(`/banner/uploadBanner?token=${this.$cookie.get('token')}`)"
      :limit="1"
      list-type="picture-card"
      :file-list="dataForm.logoImg"
      :on-preview="handlePreview"
      :on-success="handleSuccess"
      :on-remove="handleRemove"
      :on-exceed="onExceed">
      <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        dataForm: {
          id: 0,
          hotWords: '',
          hotSrc: '',
          hotType: '',
          hotSort: '',
          hotClick: '',
          hotUrl: '',
          logoImg: [],
          typeId: '',
          hotSrcOption: [
            {
              label: '首页',
              value: 1
            }, {
              label: '福利商城',
              value: 2
            }, {
              label: '联盟店',
              value: 4
            }, {
              label: '联盟店商品',
              value: 5
            }, {
              label: '采购',
              value: 6
            }
          ],
          hotTypeOption: [
            {
              label: '文字',
              value: 1
            }, {
              label: '外链',
              value: 2
            }
          ],
          typeIdOption: [
            {
              label: '热词',
              value: 1
            }, {
              label: '内部广告',
              value: 2
            }
          ]
        },
        dataRule: {
          hotSrc: [
            { required: true, message: '平台类型不能为空', trigger: 'blur' }
          ],
          hotType: [
            { required: true, message: '热词类型不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 图片预览
      handlePreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      // 文件上传成功的钩子函数
      handleSuccess (res, file, fileList) {
        this.$message({
          type: 'info',
          message: '图片上传成功',
          duration: 6000
        })
        if (res.code === 0) {
          this.dataForm.logoImg.push({name: 'tu', url: res.data})
        }
      },
      // 删除文件之前的钩子函数
      handleRemove (file, fileList) {
        for (var i = 0; i < this.dataForm.logoImg.length; i++) {
          var picc = this.dataForm.logoImg[i]
          if (picc.url === file.url) {
            this.dataForm.logoImg.splice(i, 1)
          }
        }
        this.$message({
          type: 'info',
          message: '已删除原有图片',
          duration: 6000
        })
      },
      // 上传的文件个数超出设定时触发的函数
      onExceed (files, fileList) {
        this.$message({
          type: 'info',
          message: '最多只能上传1个图片',
          duration: 6000
        })
      },
      init (id) {
        this.dataForm.hotWords = ''
        this.dataForm.hotSrc = ''
        this.dataForm.hotType = ''
        this.dataForm.hotSort = ''
        this.dataForm.hotClick = ''
        this.dataForm.hotUrl = ''
        this.dataForm.logoImg = []
        this.dataForm.typeId = ''
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/search/getHotWordsDetails/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.hotWords = data.data.hotWords
                if (data.data.logoImg !== null && data.data.logoImg !== '') {
                  this.dataForm.logoImg.push({name: 'tu', url: 'http://image.yilianbaihui.cn/consumer/' + data.data.logoImg})
                }
                this.dataForm.hotSrc = data.data.hotSrc
                this.dataForm.hotType = data.data.hotType
                this.dataForm.hotSort = data.data.hotSort
                this.dataForm.hotClick = data.data.hotClick
                this.dataForm.hotUrl = data.data.hotUrl
                this.dataForm.typeId = data.data.typeId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        let img = ''
        if (this.dataForm.logoImg.length > 0) {
          img = 'banner/' + this.dataForm.logoImg[0].url.substring(this.dataForm.logoImg[0].url.lastIndexOf('/') + 1, this.dataForm.logoImg[0].url.length)
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/search/${!this.dataForm.id ? 'addHotWords' : 'modifyHotWords'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'hotWords': this.dataForm.hotWords,
                'logoImg': img,
                'hotSrc': this.dataForm.hotSrc,
                'hotType': this.dataForm.hotType,
                'hotSort': this.dataForm.hotSort,
                'hotUrl': this.dataForm.hotUrl,
                'typeId': this.dataForm.typeId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 限制文字的长度
      widthCheck (str, len) {
        if (len === 1) {
          len = 16
        } else if (len === 2) {
          len = 30
        }
        var temp = 0
        for (var i = 0; i < str.value.length; i++) {
          if (/[\u4e00-\u9fa5]/.test(str.value[i])) {
            temp += 2
          } else {
            temp++
          }
          if (temp > len) {
            str.value = str.value.substr(0, i)
          }
        }
      }
    }
  }
</script>
